const indexConfig = require('./mock/index')

const Koa = require('koa')
const Router = require('koa-router')
const bodyParser = require('koa-bodyparser')

const app = new Koa()
var router = new Router()

app.use(bodyParser())

function responseRes(fn, ctx) {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve(fn(ctx))
        }, 1500)
    })
}

indexConfig.forEach((item) => {
    const { url, method, response } = item
    router[method](url, async (ctx, next) => {
        if (method === 'get') {
            // console.log(ctx.query)
        } else {
            // console.log(ctx)
        }
        const res = await responseRes(response, ctx)
        // console.log(res)
        ctx.body = res
    })
})

app.use(router.routes())
app.listen(3001)
